import 'package:shared_preferences/shared_preferences.dart';

class GetToken {
  static final GetToken _instance = GetToken._internal();
  factory GetToken() => _instance;

  String? _token;
  bool get isAuthenticated => _token != null;

  GetToken._internal() {
    _init();
  }

  Future<void> _init() async {
    await _loadToken();
  }




  // 从本地存储加载token
  Future<void> _loadToken() async {
    final prefs = await SharedPreferences.getInstance();
    _token = prefs.getString('token');
    // print('从本地加载token: $_token');
  }

  // 保存token
  Future<void> saveToken(String token) async {
    _token = token;
    final prefs = await SharedPreferences.getInstance();
    await prefs.setString('token', token);
    print('保存token成功: $_token');
  }

  // 清除token
  Future<void> clearToken() async {
    _token = null;
    final prefs = await SharedPreferences.getInstance();
    await prefs.remove('token');
    print('清除token成功');
  }

  // 获取token
  String? getToken() {
    return _token;
  }
}